<template>
  <div>
    <div class="operation-btn">
      <div class="dis-flex flex-x-end ">
        <el-button type="primary" size="mini" @click="saveDetailed">生成采购明细</el-button>
        <el-button type="success" plain size="mini" @click="generate">生成采购单</el-button>
        <el-button type="primary" plain size="mini" icon="el-icon-edit" @click="is_edit = !is_edit">{{ is_edit?'退出':'' }}编辑</el-button>
      </div>
    </div>
    <el-table ref="table" :data="list.data" style="min-width: 100%" size="small" :height="minHeight" :span-method="objectSpanMethod" row-key="id" class="table" @selection-change="selectionChange">
      <el-table-column type="selection" min-width="30" :reserve-selection="true" />
      <el-table-column label="来源" min-width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.goods_id">
            <el-link type="primary" :underline="false">{{ scope.row.goods_sku_code }}</el-link>
          </div>
          <div v-else> 原材料 </div>
        </template>
      </el-table-column>
      <el-table-column label="图片" min-width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.goods_id">
            <el-image v-viewer :src="scope.row.goods_image" />
          </div>
          <div v-else>
            <el-image v-viewer :src="scope.row.bom_imgurls?scope.row.bom_imgurls[0]:''">
              <div slot="error" class="image-slot" />
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="原料类别/单位" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.sort_name }}/ {{ scope.row.use_unit }}
        </template>
      </el-table-column>
      <el-table-column label="原料简称" min-width="150" prop="bom_name" />
      <el-table-column label="原料编码" min-width="150" prop="bom_sku_code" />
      <el-table-column label="颜色" min-width="60" prop="color_name" />
      <el-table-column label="规格" min-width="80" prop="spec_two_value" />
      <el-table-column label="单件用量" min-width="80" prop="use_amount" />
      <el-table-column label="单件成本" min-width="80" prop="price" />
      <el-table-column label="损耗(%)" min-width="80" prop="wastage" />
      <el-table-column label="损溢(%)" min-width="80" prop="profit_loss">
        <template slot-scope="scope">
          <div class="dis-flex flex-y-center">
            <el-input v-if="is_edit" v-model="scope.row.profit_loss" size="mini" />
            <label v-else>{{ scope.row.profit_loss }}</label>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="采购需求总量" min-width="100" prop="use_amount_all" />
      <el-table-column label="操作" min-width="200">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false">锁定原材料</el-link>
          <el-link type="primary" :underline="false" @click="addMaterials(scope.row.bom_sku_code)">单独新增原料</el-link>
          <el-link type="danger" :underline="false" @click="delValue(scope.$index)">忽略</el-link>
        </template>
      </el-table-column>
    </el-table>
    <Pagination ref="pagination" />
    <!-- 明细 -->
    <Detailed :unique.sync="unique" />
  </div>
</template>
<script>
import { PurchaseCart } from '@/api/stock/cart'
import Pagination from '@/components/Pagination'
import { getMinH } from '@/utils/public'
import Detailed from './detailed'
export default {
  name: 'GoodsCard',
  components: {
    Pagination,
    Detailed
  },
  data() {
    return {
      list: {},
      is_edit: false,
      select: [],
      spanArr: [],
      pos: 0,
      minHeight: 500,
      unique: [],
      pagination: {
        page: 1,
        per_page: 30
      }
    }
  },
  created() {
    this.getCart()
  },
  methods: {
    getCart() {
      PurchaseCart.pageGoodsBomCart(this.pagination).then((res) => {
        if (res.data.data) {
          res.data.data.forEach(ele => {
            if (!ele.profit_loss) {
              ele.profit_loss = 0
            }
          })
        }
        this.list = res.data
        this.unique = res.data.data_unique
        this.merge(this.list.data)
        this.initPagination({
          page: res.data.current_page,
          total: res.data.total,
          per_page: res.data.per_page
        })
      }).catch(() => {})
    },
    // 分页
    initPagination(data) {
      this.$refs.pagination.init({ ...data, cb: (res) => {
        this.pagination = res
        this.getCart()
      } })
    },
    show() {
      this.$nextTick(() => {
        this.minHeight = getMinH(this, 'table', 'pagination')
        this.$refs.table.doLayout()
      })
    },
    // 合并单元格
    objectSpanMethod({
      row,
      column,
      rowIndex,
      columnIndex
    }) {
      if (columnIndex === 1) {
        const _row = this.spanArr[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    // 合并单元格
    merge(data) {
      this.spanArr = []
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1)
          this.pos = 0
        } else {
          // 商品
          if (data[i].goods_sku_code) {
            if (data[i].goods_sku_code === data[i - 1].goods_sku_code) {
              this.spanArr[this.pos] += 1
              this.spanArr.push(0)
            } else {
              this.spanArr.push(1)
              this.pos = i
            }
          } else {
            this.spanArr.push(1)
            this.pos = i
          }
        }
      }
    },
    // 选择
    selectionChange(selection) {
      this.select = selection
    },
    // 忽略
    delValue(index) {
      this.list.data.splice(index, 1)
    },
    // 生成采购单
    generate() {
      if (!this.select.length) {
        this.$message.error('请先选择要操作的数据,在进行操作！')
        return false
      }
      PurchaseCart.autoPurchaseUnify({
        type: 'bom',
        storage_sn: 'CK202107211626846892',
        data: this.select
      }).then((res) => {
        this.getCart()
        this.$emit('success', res.data.id)
      }).catch(() => {})
    },
    // 生成采购单明细
    saveDetailed() {
      if (!this.select.length) {
        this.$message.error('请先选择要删除的数据,在进行操作！')
        return false
      }
      PurchaseCart.listGoodsBomMerge({
        data: this.select
      }).then((res) => {
        this.unique = res.data
      }).catch(() => {})
    },
    // 转原料
    addMaterials(bom_sku_ids) {
      PurchaseCart.addPurchaseCart({ bom_sku_ids }).then((res) => {
        this.$message.success('操作成功')
        this.getCart()
      }).catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>
.table{
  margin-top: 0px;
  .use_unit{
    margin-left: 5px;
  }
  .el-image{
    width: 60px;
  }
}
.operation-btn{
 position: absolute;
 right: 10px;
 top: 15px;
 background: white;
height: 40px;
z-index: 2;
 min-width: 80%;
}
</style>
